﻿#include "infochange.h"
#include "ui_infochange.h"
#include "setup2wind.h"

class ReadOnlyDelegate: public QItemDelegate
{

public:
    ReadOnlyDelegate(QWidget *parent = NULL):QItemDelegate(parent)
    {}

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                          const QModelIndex &index) const override //final
    {
        Q_UNUSED(parent)
        Q_UNUSED(option)
        Q_UNUSED(index)
        return NULL;
    }
};

infoChange::infoChange(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::infoChange)
{
    ui->setupUi(this);
    setWindowIcon(QIcon(":/pic/picture/kafavicon.ico"));
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40);
    ui->tableWidget->setInputMethodHints(Qt::ImhHiddenText);// 表格单击进入
    this->setAttribute(Qt::WA_QuitOnClose, false);
    // 遍历每一行，并设置行高为50
    for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
        ui->tableWidget->setRowHeight(i, 50);
    }
    this->setAttribute(Qt::WA_QuitOnClose, false);
}

infoChange::~infoChange()
{
    delete ui;
}

void infoChange::showEvent(QShowEvent *event)
{
    refTableData();
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40);
    // 遍历每一行，并设置行高为50
    for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
        ui->tableWidget->setRowHeight(i, 50);
    }
}

// 下拉选切换
void infoChange::on_comboBox_currentTextChanged(const QString &arg1)
{
    refTableData();
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40);
    // 遍历每一行，并设置行高为50
    for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
        ui->tableWidget->setRowHeight(i, 50);
    }
}

// 添加
void infoChange::add_clicked(){
    QString dicCode;
    if(ui->comboBox->currentText()=="样本名称"){
        dicCode = "1";
    }else if(ui->comboBox->currentText()=="样本类型"){
        dicCode = "2";
    }else if(ui->comboBox->currentText()=="扩增体积"){
        dicCode = "4";
    }else if(ui->comboBox->currentText()=="耗材类型"){
        dicCode = "6";
    }else if(ui->comboBox->currentText()=="试剂盒"){
        dicCode = "12";
    }else if(ui->comboBox->currentText()=="通道一"){
        dicCode = "7";
    }else if(ui->comboBox->currentText()=="通道二"){
        dicCode = "8";
    }else if(ui->comboBox->currentText()=="通道三"){
        dicCode = "9";
    }else if(ui->comboBox->currentText()=="通道四"){
        dicCode = "10";
    }else if(ui->comboBox->currentText()=="内标"){
        dicCode = "11";
    }

    QPushButton *btn = (QPushButton*)sender();
    QWidget *w_parent = (QWidget*)btn->parent();
    int x = w_parent->frameGeometry().x();
    int y = w_parent->frameGeometry().y();
//    QModelIndex index = ui->tableWidget->indexAt(QPoint(x,y));
//    int id = ui->tableWidget->item(index.row(),0)->text().toInt();

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Insert into ItemCode values('%1','%2','','0')").arg(dicCode,QString::number(ui->tableWidget->rowCount()+1));
    query.exec(sql);
    if(dicCode=="7"||dicCode=="8"||dicCode=="9"||dicCode=="10"){
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql=QString("Insert into ItemCode values('%1','%2','','0')").arg("11",QString::number(ui->tableWidget->rowCount()+1));
        query.exec(sql);
    }
    refTableData();
    setup2wind::typechangefl.set(setup2wind::typechangefl.get()+1);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40);
    // 遍历每一行，并设置行高为50
    for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
        ui->tableWidget->setRowHeight(i, 50);
    }
}

// 默认
void infoChange::set_clicked(){

    QString dicCode;
    if(ui->comboBox->currentText()=="样本名称"){
        dicCode = "1";
    }else if(ui->comboBox->currentText()=="样本类型"){
        dicCode = "2";
    }else if(ui->comboBox->currentText()=="扩增体积"){
        dicCode = "4";
    }else if(ui->comboBox->currentText()=="耗材类型"){
        dicCode = "6";
    }else if(ui->comboBox->currentText()=="试剂盒"){
        dicCode = "12";
    }else if(ui->comboBox->currentText()=="通道一"){
        dicCode = "7";
    }else if(ui->comboBox->currentText()=="通道二"){
        dicCode = "8";
    }else if(ui->comboBox->currentText()=="通道三"){
        dicCode = "9";
    }else if(ui->comboBox->currentText()=="通道四"){
        dicCode = "10";
    }else if(ui->comboBox->currentText()=="内标"){
        dicCode = "11";
    }

    QPushButton *btn = (QPushButton*)sender();
    QWidget *w_parent = (QWidget*)btn->parent();
    int x = w_parent->frameGeometry().x();
    int y = w_parent->frameGeometry().y();
    QModelIndex index = ui->tableWidget->indexAt(QPoint(x,y));
    QString id = ui->tableWidget->item(index.row(),0)->text();

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql=QString("SELECT Name FROM ItemCode WHERE DicCode='%1' and IsDefault ='1' ").arg(dicCode);
    QString isname;
    query.exec(sql);
    while (query.next()) {
        QString column2Value = query.value("Name").toString();
        isname.append(column2Value);
    }

    sql=QString("UPDATE ItemCode SET IsDefault =0 WHERE DicCode='%1'").arg(dicCode);
    query.exec(sql);
    if(dicCode == "11"&&id==isname){
        sql=QString("UPDATE ItemCode SET IsDefault ='0' WHERE DicCode='%1' and Code= '%2'").arg(dicCode,id);
        query.exec(sql);
    }else{
        sql=QString("UPDATE ItemCode SET IsDefault ='1' WHERE DicCode='%1' and Code= '%2'").arg(dicCode,id);
        query.exec(sql);
    }
    refTableData();
    setup2wind::typechangefl.set(setup2wind::typechangefl.get()+1);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40);
    // 遍历每一行，并设置行高为50
    for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
        ui->tableWidget->setRowHeight(i, 50);
    }
}

// 删除
void infoChange::del_clicked(){

    QString dicCode;
    if(ui->comboBox->currentText()=="样本名称"){
        dicCode = "1";
    }else if(ui->comboBox->currentText()=="样本类型"){
        dicCode = "2";
    }else if(ui->comboBox->currentText()=="扩增体积"){
        dicCode = "4";
    }else if(ui->comboBox->currentText()=="耗材类型"){
        dicCode = "6";
    }else if(ui->comboBox->currentText()=="试剂盒"){
        dicCode = "12";
    }else if(ui->comboBox->currentText()=="通道一"){
        dicCode = "7";
    }else if(ui->comboBox->currentText()=="通道二"){
        dicCode = "8";
    }else if(ui->comboBox->currentText()=="通道三"){
        dicCode = "9";
    }else if(ui->comboBox->currentText()=="通道四"){
        dicCode = "10";
    }else if(ui->comboBox->currentText()=="内标"){
        dicCode = "11";
    }

    QPushButton *btn = (QPushButton*)sender();
    QWidget *w_parent = (QWidget*)btn->parent();
    int x = w_parent->frameGeometry().x();
    int y = w_parent->frameGeometry().y();
    QModelIndex index = ui->tableWidget->indexAt(QPoint(x,y));
    QString id = ui->tableWidget->item(index.row(),0)->text();

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql=QString("SELECT IsDefault FROM ItemCode WHERE DicCode=4 and Code='%1'").arg(id);
    query.exec(sql);

    bool isDefault=false;
    while (query.next()) {
        isDefault = query.value("IsDefault").toString() =="1";
    }
    if(isDefault){
        QMessageBox::warning(this,"警告"," 无法删除默认数据;",QMessageBox::Ok);
        return;
    }
    sql=QString("DELETE FROM ItemCode WHERE Code = '%1' and DicCode = '%2'").arg(id,dicCode);
    query.exec(sql);
    if(dicCode=="7"||dicCode=="8"||dicCode=="9"||dicCode=="10"){
        sql=QString("DELETE FROM ItemCode WHERE Code = '%1' and DicCode = '%2'").arg(id,"11");
        query.exec(sql);
    }
    refTableData();
    setup2wind::typechangefl.set(setup2wind::typechangefl.get()+1);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40);
    // 遍历每一行，并设置行高为50
    for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
        ui->tableWidget->setRowHeight(i, 50);
    }
}

void infoChange::on_tableWidget_cellChanged(int row, int column)
{
    qDebug()<<row<<column;
    QString dicCode;
    if(ui->comboBox->currentText()=="样本名称"){
        dicCode = "1";
    }else if(ui->comboBox->currentText()=="样本类型"){
        dicCode = "2";
    }else if(ui->comboBox->currentText()=="扩增体积"){
        dicCode = "4";
    }else if(ui->comboBox->currentText()=="耗材类型"){
        dicCode = "6";
    }else if(ui->comboBox->currentText()=="试剂盒"){
        dicCode = "12";
    }else if(ui->comboBox->currentText()=="通道一"){
        dicCode = "7";
    }else if(ui->comboBox->currentText()=="通道二"){
        dicCode = "8";
    }else if(ui->comboBox->currentText()=="通道三"){
        dicCode = "9";
    }else if(ui->comboBox->currentText()=="通道四"){
        dicCode = "10";
    }else if(ui->comboBox->currentText()=="内标"){
        dicCode = "11";
    }
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString code=ui->tableWidget->item(row,0)->text();
    QString infoname=ui->tableWidget->item(row,column)->text();

    QString sql=QString("Update ItemCode set Name='%1' Where DicCode='%2' and Code='%3'").arg(infoname,dicCode,code);
    query.exec(sql);
    if(dicCode=="7"||dicCode=="8"||dicCode=="9"||dicCode=="10"){
        QString sql=QString("Update ItemCode set Name='%1' Where DicCode='%2' and Code='%3'").arg(infoname,"11",code);
        query.exec(sql);
    }
    refTableData();
    setup2wind::typechangefl.set(setup2wind::typechangefl.get()+1);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40);
    // 遍历每一行，并设置行高为50
    for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
        ui->tableWidget->setRowHeight(i, 50);
    }
}

void infoChange::refTableData(){
    ui->tableWidget->blockSignals(true);
    QString dicCode;
    if(ui->comboBox->currentText()=="样本名称"){
        dicCode = "1";
    }else if(ui->comboBox->currentText()=="样本类型"){
        dicCode = "2";
    }else if(ui->comboBox->currentText()=="扩增体积"){
        dicCode = "4";
    }else if(ui->comboBox->currentText()=="耗材类型"){
        dicCode = "6";
    }else if(ui->comboBox->currentText()=="试剂盒"){
        dicCode = "12";
    }else if(ui->comboBox->currentText()=="通道一"){
        dicCode = "7";
    }else if(ui->comboBox->currentText()=="通道二"){
        dicCode = "8";
    }else if(ui->comboBox->currentText()=="通道三"){
        dicCode = "9";
    }else if(ui->comboBox->currentText()=="通道四"){
        dicCode = "10";
    }else if(ui->comboBox->currentText()=="内标"){
        dicCode = "11";
    }

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql=QString("SELECT Code,Name,IsDefault FROM ItemCode WHERE DicCode='%1'").arg(dicCode);
    QStringList codes,sanames,isds;
    query.exec(sql);
    while (query.next()) {
        QString column1Value=query.value("Code").toString();
        QString column2Value = query.value("Name").toString();
        QString column3Value = query.value("IsDefault").toString();
        codes.append(column1Value);
        sanames.append(column2Value);
        isds.append(column3Value);
    }

    ui->tableWidget->setRowCount(sanames.length());
    ui->tableWidget->setColumnCount(3);
    ui->tableWidget->setWindowTitle("QTableWidget & Item");
    ui->tableWidget->resizeColumnsToContents();
    ui->tableWidget->resizeRowsToContents();
    QStringList header;
    header.append("");
    header.append("信息");
    header.append("操作");
    ui->tableWidget->setHorizontalHeaderLabels(header);
    ui->tableWidget->verticalHeader()->setVisible(false);
    ui->tableWidget->setColumnHidden(0, true); // 隐藏第1列

    for(int i=0;i<sanames.length();i++){
        QPushButton *btn_1 = new QPushButton();
        QPushButton *btn_2 = new QPushButton();
        QPushButton *btn_3 = new QPushButton();
        btn_1->setText(tr("添加"));
        btn_2->setText(tr("默认"));
        btn_3->setText(tr("删除"));
        if(isds[i]=="1"){
            btn_2->setStyleSheet("background-color: rgb(173, 216, 230);");
        }
        btn_1->setMinimumSize(60,30);
        btn_2->setMinimumSize(60,30);
        btn_3->setMinimumSize(60,30);
        QWidget *tmp_widget = new QWidget();
        QHBoxLayout *tmp_layout = new QHBoxLayout(tmp_widget);
        if(dicCode == "11"){
            tmp_layout->addWidget(btn_2);
        }else{
            tmp_layout->addWidget(btn_1);
            tmp_layout->addWidget(btn_2);
            tmp_layout->addWidget(btn_3);
        }
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(codes[i]));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(sanames[i]));
        ui->tableWidget->setCellWidget(i,2,tmp_widget);
        connect(btn_1,SIGNAL(clicked()),this,SLOT(add_clicked()));
        connect(btn_2,SIGNAL(clicked()),this,SLOT(set_clicked()));
        connect(btn_3,SIGNAL(clicked()),this,SLOT(del_clicked()));
    }
    // 表头颜色
    ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}");
    ui->tableWidget->show();
    ui->tableWidget->blockSignals(false);
    if(dicCode == "11"){
        for(int i=0;i<ui->tableWidget->rowCount();i++){
            ReadOnlyDelegate* readOnlyDelegate = new ReadOnlyDelegate(this);
            ui->tableWidget->setItemDelegateForColumn(i, readOnlyDelegate);
        }
    }else{
        for(int i=0;i<ui->tableWidget->rowCount();i++){
            ui->tableWidget->setItemDelegateForColumn(i, nullptr);
        }
    }
    // 遍历每一行，并设置行高为50
    for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
        ui->tableWidget->setRowHeight(i, 50);
    }

}

void infoChange::on_pushButton_clicked()
{
    this->close();
}
